//
//  UserAccountInfoModel.swift
//  weibo09
//
//  Created by Jet on 16/5/8.
//  Copyright © 2016年 Jet. All rights reserved.
//

import UIKit
//归档：（对象小数据持久化的方法）
//１.遵守NSCoding　协议　　２.实现两个方法

class UserAccountInfoModel: NSObject ,NSCoding{
    
        
    //用于调用access_token，接口获取授权后的access_token
    var access_token:String?
    //	access_token的生命周期，单位是秒数。
    var expires_in:NSTimeInterval=0{
        didSet{
            //timeIntervalSinceNow在当前的时间基础上叠加一个时间得到过期时间
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //token过期时间
    var expires_date:NSDate?
    
    //授权用户的UID
    var uid:String?
    //用户头像地址　String
    var avatar_large:String?
    //用户名
    var name:String?
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
    
        let keys = ["access_token","expires_in","uid","avatar_large","name","expires_date"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    func saveAccount(){
        //获取路径account.plist
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        //有值返回
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        print(path)
    }
    
    
    //对外界提供归档方法
    class func loadAccount()->UserAccountInfoModel?{
     //生成路径和创建account.plist
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        //有值返回
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as?UserAccountInfoModel{
            //判断用户token是否过期,不过期才返回数据
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
               return account
            }
        }
        //没数据返回nil
        return nil
    }
    
   // MARK: - 实现NSCoding协议的方法
   //１.解归档　将磁盘中的二进制数据－》转换成自定义的对象　(类似于反序列化)
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token")as?String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid")as?String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large")as?String
        name = aDecoder.decodeObjectForKey("name")as?String
         expires_date = aDecoder.decodeObjectForKey("expires_date")as?NSDate

    }
    //归档　将对象转换成二进制　保存到磁盘中　（类似序列化）
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
        
    }
    
    
    
    
    
}
